package fr.unice.polytech.chord.core;

import java.io.Serializable;
import java.math.BigInteger;

/**
 * Table that contains {@link Finger}. The number of fingers depends of the size
 * of the {@link Key} used to identify peers. The number of finger entries is
 * {@link Key#NB_BITS}.
 * 
 * @author Anthony Parisy
 * @author Laurent Pellegrino
 * @author Marc Valdener
 * 
 * @version 10/28/2009
 */
@SuppressWarnings("serial")
public class FingerTable implements Serializable {

	Finger[] fingers = new Finger[Key.NB_BITS];

	/**
	 * Constructor.
	 * 
	 * @param node
	 *            the peer to which this finger table is associated.
	 */
	public FingerTable(ChordNode node) {
		for (int i = 0; i < Key.NB_BITS; i++) {
			this.fingers[i] = new Finger(node.getKey().getValue().add(
					BigInteger.valueOf(2).pow(i)).mod(
					BigInteger.valueOf(2).pow(Key.NB_BITS)), node);
		}
	}

	/**
	 * Returns the finger at the specified position.
	 * 
	 * @param index
	 *            the position.
	 * @return the finger found at the specified position.
	 */
	public Finger getFinger(int index) {
		return this.fingers[index];
	}

	/**
	 * Returns all the fingers.
	 * 
	 * @return all the fingers.
	 */
	public Finger[] getFingers() {
		return this.fingers;
	}

}
